//
//  NSString+BloodSugar.m
//  FMDataBaseDemo
//
//  Created by liliang on 14-5-20.
//  Copyright (c) 2014年 liliang. All rights reserved.
//
//一些转换相关的方法

#import "NSString+BloodSugar.h"

@implementation NSString (BloodSugar)

+ (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)shortStringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)dateStampString:(NSString *)fileExtension {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat    = @"yyyyMMddHHmmss";
    NSString *stamp =[NSString stringWithFormat:@"%@%04d", [dateFormatter stringFromDate:[NSDate date]], rand()%10000];
    if (!fileExtension) {
        return stamp;
    }
    return [stamp stringByAppendingPathExtension:fileExtension];
}

@end


@implementation NSDate (BloodSugar)

+ (NSDate *)dateFromString:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return date;
}

+ (NSDate *)dateFromShortString:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return date;
}


+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:format];
    NSDate *date = [inputFormatter dateFromString:string];
    return date;
}

@end